home *** CD-ROM | disk | FTP | other *** search
- /*
- File: EnableIPReuseAddrSample.c
- By: Rich Kubota
- Developer Technical Support
-
- Purpose: Demonstrate the use of the OTOptionManagement call to tell a TCP
- endpoint that the address may be reused immediately after closing
- the endpoint. This allows the endpoint to be reopened to the
- same address. Under OT, TCP has a 2-minute timeout on a binding
- after a connection has closed before the same port can be bound
- to again. This prevents data from stale connections from
- corrupting a new one.
- */
- #include <OpenTransport.h> // open transport files
- #include <OpenTptInternet.h>
-
- OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
-
- /* Input: ep - endpointref on which to negotiate the option
- enableReuseIPMode - desired option setting - true/false
- Return: kOTNoError indicates that the option was successfully negotiated
- OSStatus is an error if < 0, otherwise, the status field is
- returned and is > 0.
-
- IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise
- this code will not function as desired
- */
-
-
- OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode)
-
- {
- UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size
- TOption* opt; // option ptr to make items easier to access
- TOptMgmt req;
- TOptMgmt ret;
- OSStatus err;
-
- if (!OTIsSynchronous(ep))
- {
- return (-1);
- }
- opt = (TOption*)buf; // set option ptr to buffer
- req.opt.buf = buf;
- req.opt.len = sizeof(buf);
- req.flags = T_NEGOTIATE; // negotiate for option
-
- ret.opt.buf = buf;
- ret.opt.maxlen = kOTFourByteOptionSize;
-
- opt->level = INET_IP; // dealing with an IP Level function
- opt->name = IP_REUSEADDR;
- opt->len = kOTFourByteOptionSize;
- opt->status = 0;
- *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false
-
- err = OTOptionManagement(ep, &req, &ret);
-
- // if no error then return the option status value
- if (err == kOTNoError)
- {
- if (opt->status != T_SUCCESS)
- err = opt->status;
- else
- err = kOTNoError;
- }
-
- return err;
- }
-